import { Navigate, createBrowserRouter } from "react-router-dom";
import App from "../App.jsx";
import Study from "../pages/Study/Study.jsx";
import Quan from "../pages/Quan/Quan.jsx";
import Login from "../pages/Login/Login.jsx";
import Jing from "../pages/Jing/Jing.jsx";
import Mine from "../pages/Mine/Mine.jsx";
import AI from "../pages/AI/AI.jsx";
import Dindan from "../pages/wddd/dindan.jsx";
import Forget from "../pages/Login/Forget.jsx";
import Reset from "../pages/Login/Reset.jsx";
import Course from "../pages/Study/Course.jsx";
import Detail from "../pages/Study/Detail.jsx";
import Test from "../pages/Study/Test.jsx"
import Wrong from "../pages/Study/Wrongs.jsx"
import Collect from "../pages/Study/Collection.jsx"
import Materials from "../pages/Study/Materials.jsx"
import Practice from "../pages/Study/Practice.jsx"

// 认证守卫组件
const AuthGuard = ({ children }) => {
  const token = localStorage.getItem("token") || null;
  if (!token) {
    return <Navigate to="/login" />;
  }
  return children;
}

const router = createBrowserRouter([
  {
    path: "/",
    element: <App />,
    children: [
      {
        path: "/",
        element: <Navigate to="/study" />,
      },
      {
        path: "/study",
        element: <AuthGuard><Study /></AuthGuard>,
      },
      {
        path: "/quan",
        element: <AuthGuard><Quan /></AuthGuard>,
      },
      {
        path: "/jing",
        element: <AuthGuard><Jing /></AuthGuard>,
      },
      {
        path: "/mine",
        element: <AuthGuard><Mine /></AuthGuard >,
      },
    ],
  },
  {
    path: "/login",
    element: <Login />,
  },
  {
    path: "/ai",
    element: <AI />,
  }, 
  {
    path: "/dingdan",
    element: <Dindan />,
  }
    path: "/forget",
    element: <Forget />,
  },
  {
    path: "/reset",
    element: <Reset />,
  },
  {
    path: "/course",
    element: <AuthGuard><Course /></AuthGuard>,
  },
  {
    path: "/detail",
    element: <AuthGuard><Detail /></AuthGuard>,
  },
  {
    path: "/test",
    element: <AuthGuard><Test /></AuthGuard>
  },
  {
    path: "/wrong",
    element: <AuthGuard><Wrong /></AuthGuard>,
  },
  {
    path: "/collect",
    element: <AuthGuard><Collect /></AuthGuard>,
  },
  {
    path: "/materials",
    element: <AuthGuard><Materials /></AuthGuard>,
  },
  {
    path: "/practice",
    element: <AuthGuard><Practice /></AuthGuard>,
  },
]);

export default router;
